package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.RoleDao;
import com.aaa.entity.SysRoleEntity;
import com.aaa.entity.Pager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RoleDaoImpl extends BaseDao implements RoleDao {

    @Override
    public Map queryById(int role_id) {
        String sql="select * from sys_role where role_id=?";
        List list= this.executeQuery(sql,role_id);
        if(list.size()>0)
            return (Map) list.get(0);
        return null;
    }

    @Override
    public int update(SysRoleEntity info) {
        String sql="update sys_role set role_name=?,role_key=?,status=?,remark=? where role_id=?";
        return this.executeUpdate(sql,info.getRoleName(),info.getRoleKey(),info.getStatus(),info.getRemark(),info.getRoleId());
    }

    @Override
    public int delete(int id) {
        return 0;
    }

    @Override
    public int create(SysRoleEntity info) {
        String sql="insert into sys_role (role_name,role_key,status,remark) value(?,?,?,?)";
        return this.executeInsert(sql,info.getRoleName(),info.getRoleKey(),info.getStatus(),info.getRemark());
    }

    @Override
    public Pager query(int curPage, int pageSize, Map condition) {
        String sql="select * from sys_role where 1=1 and del_flag=0 ";
        StringBuffer buffer=new StringBuffer(sql);
        List params=new ArrayList();

        if (condition!=null)
        {
            if (condition.containsKey("role_name")&&condition.get("role_name").toString().length()>0)
            {
                buffer.append("and role_name like ?");
                params.add("%"+condition.get("role_name")+"%");
            }
            if (condition.containsKey("role_key")&&condition.get("role_key").toString().length()>0)
            {
                buffer.append("and role_key like ?");
                params.add("%"+condition.get("role_key")+"%");
            }
            if (condition.containsKey("status")&&condition.get("status").toString().length()>0)
            {
                buffer.append("and status like ?");
                params.add("%"+condition.get("status")+"%");
            }
        }

        sql="select count(*) c from ("+buffer+") role";
        List<Map> map=this.executeQuery(sql,params.toArray());
        Long count=(long)map.get(0).get("c");

        buffer.append("limit ?,?");
        int startIndex=(curPage-1)*pageSize;
        params.add(startIndex);
        params.add(pageSize);
        List<Map> list=this.executeQuery(buffer.toString(),params.toArray());

        Pager pager=new Pager(curPage,pageSize,list,count.intValue());
        return pager;


    }

    @Override
    public Map queryid(int role_id) {
        String sql="select * from sys_role where role_id=?";
        List<Map> list=this.executeQuery(sql,role_id);
        return list.size()>0?list.get(0):null;
    }

    @Override
    public int deleteMenusByRoleId(int role_id) {
        String sql="update sys_role set del_flag=1 where role_id=?";
        return this.executeUpdate(sql,role_id);
    }

    @Override
    public List queryMenusByRoleId(int roleid) {
        String sql="select m.menu_id,m.parent_id,m.menu_name,1 as open, \n" +
                "(case when role_id is not null then true else false end) as checked \n" +
                "from sys_menu m left join \n" +
                "(select * from sys_role_menu rm where rm.role_id=? ) as rm \n" +
                "on m.menu_id=rm.menu_id";
        return executeQuery(sql,roleid);
    }

    @Override
    public int addRoleMenu(int roleid, int menuid) {
        String sql="insert into sys_role_menu (role_id,menu_id) values(?,?)";
        return this.executeUpdate(sql,roleid,menuid);
    }

    @Override
    public List query() {
        String sql="select role_id,role_name from sys_role where del_flag=0 and status=0";
        return this.executeQuery(sql);
    }
}
